﻿using System;
using System.Linq;
using Grishko.DataTier;
using OutdoorLib.Context;
using Grishko.SerilizeTools;


namespace OutdoorLib.BaseEntity.Contract
{
    /// <summary>
    /// Платежи по контракту
    /// </summary>
    public class Payments : EntityList
    {
        
        public double  Summa
        {
            get
            {                
              return  this.ToArray <PaymentItem>().Sum(w=>w.Summa);  
            }
        }

        /// <summary>
        /// Вернуть сумму платежей на опр. дату
        /// </summary>
        public double SummaForDate(DateTime dt)
        {
            PaymentItem[] Source = this.ToArray<PaymentItem>();

            var res =
            from p in Source
            where p.Date <dt || p.Date ==dt
            select p.Summa;


            return res.Sum();

            

            

        }

        /// <summary>
        /// Вернуть сумму за опр. период 
        /// </summary>
        /// <param name="dtStart"></param>
        /// <param name="dtFinish"></param>
        /// <returns></returns>
        public double SummaForBetween(DateTime dtStart, DateTime dtFinish)
        {
            if (dtFinish == dtStart)
                    dtStart = AgencyLib_GlobalConstants.MinDate;

            PaymentItem[] Source = this.ToArray<PaymentItem>();

            var res =
            from p in Source
            where (p.Date > dtStart || p.Date == dtStart) && (p.Date < dtFinish || p.Date == dtFinish)
            select p.Summa;


            return res.Sum();

            
        }


        /// <summary>
        /// Вернуть отсортированный по дате список 
        /// </summary>
        /// <returns></returns>
        public PaymentItem[] GetPaymentsItems()
        {
           
            var res =
            from p in this.ToArray <PaymentItem>() 
            orderby p.Date 
            select p;

            return res.ToArray <PaymentItem>();
            
        }
    }

    /// <summary>
    /// Платеж
    /// </summary>
    
    public class PaymentItem : EntitySingle
    {
        public DateTime Date { get; set; }
        public int Summa { get; set; }
    }
}
